package com.next

import grails.converters.JSON
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import java.text.SimpleDateFormat
import org.grails.web.util.WebUtils

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR'])
class ProductItemController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond ProductItem.list(params), model:[productItemCount: ProductItem.count()]
    }

    def show(ProductItem productItem) {
        respond productItem
    }

    def create() {
        def targetUri = request.getHeader("referer")
        respond new ProductItem(params), model: [targetUri: targetUri]
    }

    @Transactional
    def save(ProductItem productItem) {
        if (productItem == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (productItem.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond productItem.errors, view:'create'
            return
        }

        productItem.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'productItem.label', default: 'ProductItem'), productItem.id])
                redirect uri: params['targetUri']
            }
            '*' { respond productItem, [status: CREATED] }
        }
    }

    def edit(ProductItem productItem) {
        def targetUri = request.getHeader("referer")
        respond productItem, model: [targetUri: targetUri]
    }

    @Transactional
    def update(ProductItem productItem) {
        if (productItem == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (productItem.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond productItem.errors, view:'edit'
            return
        }

        productItem.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'productItem.label', default: 'ProductItem'), productItem.id])
                redirect uri: params['targetUri']
            }
            '*'{ respond productItem, [status: OK] }
        }
    }

    @Transactional
    def delete(ProductItem productItem) {

        if (productItem == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        productItem.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'productItem.label', default: 'ProductItem'), productItem.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'productItem.label', default: 'ProductItem'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
